package br.com.sovi.gwt.server.rest;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;

public abstract class RestController {

	private static <T> T get(String url) {
		Client client = Client.create();

		WebResource resource = client.resource(url);

		return resource.type(MediaType.APPLICATION_JSON).get(new GenericType<T>() {
		});
	}

	public static <T> T get(String url, GenericType<T> genericType) {
		Client client = Client.create();

		WebResource resource = client.resource(url);

		// PARA MOSTRAR O CONTEUDO 
//		String s = resource.type(MediaType.APPLICATION_JSON).get(String.class);
//		System.out.println("OPA: " + s);

		return resource.type(MediaType.APPLICATION_JSON).get(genericType);
	}

	public static <T> T get(String url, MultivaluedMap<String, String> queryParams) {
		Client client = Client.create();

		WebResource resource = client.resource(url);

		return resource.queryParams(queryParams).type(MediaType.APPLICATION_JSON).get(new GenericType<T>() {
		});
	}

	public static <T> T post(String url, GenericType<T> genericType, Object content) {
		Client client = Client.create();

		WebResource resource = client.resource(url);

		return resource.type(MediaType.APPLICATION_JSON).post(genericType, content);
	}

}
